package com.ruoyi.project.cms.pm.controller;

import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.cms.pm.domain.Process;
import com.ruoyi.project.cms.pm.domain.ProcessModel;
import com.ruoyi.project.cms.pm.domain.Project;
import com.ruoyi.project.cms.pm.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Joseph Wang
 * @version 1.0
 * @date 19/05/2020 16:35
 */
@Controller
@RequestMapping("/cms/pm/assign")
public class ProjectAssignController extends BaseController {
    //控制类请求地址前缀
    private String prefix = "cms/pm/assign";

    @Autowired
    private ProjectService projectService;

    @GetMapping()
    public String project() {
        return prefix + "/project";
    }

    /**
     * @return com.ruoyi.framework.web.page.TableDataInfo
     * @Author Joseph Wang
     * @Description 根据条件分页查询未分配任务项目
     * @Date 12:50 18/05/2020
     * @Param [project]
     **/
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Project project) {
        startPage();
        List<Project> list = projectService.findUnassignedProject(project);
        return getDataTable(list);
    }


    /**
     * @return java.lang.String 跳转详情页面
     * @Author Joseph Wang
     * @Description 跳转任务分配页面
     * @Date 00:43 19/05/2020
     * @Param [pid, map]
     **/
    @GetMapping("/edit/{pid}")
    public String add(@PathVariable("pid") String pid, ModelMap map) {
        map.put("name", "project");
        map.put("project", projectService.findProjectById(pid));
        return prefix + "/edit";
    }

    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addAssignment(ProcessModel processes) {
        List<Process> processList = processes.getProcesses();
        int sum = 0;
        if (processList == null || processList.size() <= 0) {
            return AjaxResult.error("未分配任务");
        } else {
            for (Process process : processList) {
                int i = projectService.insertAssignment(process);
                sum += i;
            }
        }
        return toAjax(sum);
    }
}
